package hanshunping.project.tankWar;

import hanshunping.easy.Array;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Iterator;

public class TankPanel extends JPanel implements KeyListener,Runnable {
    TankA me = new TankA(100,100);
    Object lock = new Object();
    Enemys ets = new Enemys(lock);
    int hurt = 0;
    int score = 0;
    {
        ets.start();
    }
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        this.drawTank(g);
        this.drawBullet(g);
        this.drawEnemy(g);
        g.drawString("得分：   "+score,0,10);
        g.drawString("被击中： "+hurt,0,30);
    }


    //击中对方了吗
    boolean ishit(EnemyTank et,Shot bullet){
        if(bullet.x<=et.x+25&&bullet.x>=et.x&&bullet.y<=et.y+33&&bullet.y>=et.y){
            return true;
        }else{
            return false;
        }
    }

    //被击中了吗
    boolean ishurt(TankA me,EnemyBullet eb){
        if(eb.x<=me.x+25&&eb.x>=me.x&&eb.y<=me.y+33&&eb.y>=me.y){
            return true;
        }else{
            return false;
        }
    }

    public void drawTank(Graphics g){
        g.fill3DRect(me.getX(),me.getY(),30,38,true);
    }

    public void drawEnemy(Graphics g){
        synchronized (lock) {
            for (EnemyTank e : ets.ets) {
                if(e.live){
                    g.setColor(new Color(200,200,2));
                    g.fill3DRect(e.getX(), e.getY(), 30, 38, true);
                }
                for (EnemyBullet eb : e.getBull()) {
                    if(eb.live){
                        g.setColor(new Color(150,0,150));
                        g.fill3DRect(eb.x, eb.y, 5, 5, true);}
                }
            }
        }
    }
    public void drawBullet(Graphics g) {
        synchronized (lock) {
            Iterator<Shot> itr = me.bullets.iterator();
            while (itr.hasNext()) {
                Shot bullet = itr.next();
                if (bullet.live == false) {
                    itr.remove();
                }
                if (bullet.live == true)
                    g.fill3DRect(bullet.x, bullet.y, 5, 5, true);
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch ((char)e.getKeyCode()){
            case 'W':
                me.setY(me.getY()-7);
                repaint();
                break;
            case 'A':
                me.setX(me.getX()-7);
                repaint();
                break;
            case 'S':
                me.setY(me.getY()+7);
                repaint();
                break;
            case 'D':
                me.setX(me.getX()+7);
                repaint();
                break;
            case 'J':
                me.shot();
                repaint();
                break;
            default:
                System.out.println("未设置"+(char)e.getKeyCode());
        }


    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while (true) {
            synchronized (lock) {
                Iterator<EnemyTank> itr = ets.ets.iterator();
                while (itr.hasNext()) {
                    EnemyTank et = itr.next();
                    Iterator<Shot> itrb = me.bullets.iterator();
                    while (itrb.hasNext()) {
                        Shot bullet = itrb.next();
                        if (ishit(et, bullet) && et.live) {
                            score++;
                            et.live = false;
                            bullet.live = false;
                        }
                    }
                    if(et.exist==false){
                        itr.remove();
                    }
                    for(EnemyBullet eb:et.getBull()){
                        if(ishurt(me,eb)&&eb.live){
                            hurt++;
                            eb.live = false;
                        }
                    }
                }
            }
            repaint();
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}









class runpanel implements Runnable{
    TankPanel a;
    public runpanel(TankPanel a){
        this.a = a;
    }

    @Override
    public void run() {
        while (true) {
            a.repaint();
        }
    }
}